require 'serialisation'
require 'util'

local storedData = nil

local function store_data()
    local constant = 42
    local dataToStore = 
        {
            textField = "Hello, world! 12345",
            numberField = 3.14159,
            tableField = {1,2,3},
            nilField = nil,
            referenceField = constant,
        }
    dataToStore.selfReferenceField = dataToStore
    
    storedData = serialise(dataToStore)
    --print (storedData)
    
    if (storedData) then 
        return true 
    end
end

function load_data()
    local loadedData = deserialise(storedData)
    --print (table.show(loadedData, "table"))
    
    assert(loadedData.textField == "Hello, world! 12345")
    assert(loadedData.numberField == 3.14159)
    assert(loadedData.tableField[1] == 1)
    assert(loadedData.nilField == nil)
    assert(loadedData.referenceField == 42)
    assert(loadedData.selfReferenceField == loadedData)
    
    return true
end

if tests == nil then tests = {} end

tests.serialisation = {
    store_data = {
        description = "Serialises a table",
        test = store_data,
        prerequisites = {}
    },    
    load_data = {
        description = "Deserialises a table",
        test = load_data,
        prerequisites = {"store_data"}
    },
}

